package sicau.carManager.controller;


import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import sicau.carManager.Util.ExcelUtil.ExcelUtil;
import sicau.carManager.entity.Admin;
import sicau.carManager.entity.FixedCarInformation;
import sicau.carManager.entity.ViolationInformation;
import sicau.carManager.result.R;
import sicau.carManager.service.FixedCarInformationService;
import sicau.carManager.service.ViolationInformationService;

import javax.servlet.http.HttpServletResponse;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author ITcz
 * @since 2021-02-05
 */
@Slf4j
@RestController
@RequestMapping("/violation-information")
@CrossOrigin
public class ViolationInformationController {

    @Autowired
    ViolationInformationService vioService;

    @Autowired
    FixedCarInformationService fixService;

    /**
    * 供非管理员查询车辆违章信息（没有登录校验）
    * */
    @GetMapping("select/violationInformation/by/licensePlate")
    @ApiOperation("通过车牌查询违章信息")
    public R selectByLicensePlate(Admin admin,
                                  @ApiParam("licensePlate") @RequestParam("licensePlate") String licensePlate,
                                  @ApiParam("campus, 中文名称（即雅安、温江、都江堰）") @RequestParam(name = "campus", required = false) String campus){
        //如果登录了，则从登录信息中获取校区
        if (admin != null) {
            campus = admin.getCampus();
        }
        if (licensePlate == null || campus == null) {
            return R.error(500509, "参数为空!");
        }
        List<ViolationInformation> violationInformationList = vioService.selectListByLicensePlate(licensePlate, campus);
        if (violationInformationList == null) {
            return R.ok("该车辆暂无违章信息");
        }
        HashMap resMap = new HashMap();
        resMap.put("违章信息", violationInformationList);
        return R.ok(resMap);
    }

    @GetMapping("show/than/three/times")
    @ApiOperation("显示三次以上的违章车辆信息,前端把id隐藏起来不显示，调用修改和删除数据时需要传id[返回数据有id,违章图片,违章车牌号，违章地点，违章原因，记录时间，车主联系电话]")
    public R showViolationInformationThanThreeTimes(Admin admin){
        //获得违章车辆的全部信息
        List<ViolationInformation> violationInformationList = vioService.selectListThanThreeTimes(admin.getCampus());
        if (violationInformationList == null) {
            return R.ok("暂无违章3次及其以上的车辆");
        }
        HashMap<String, Object> resMap = new HashMap();
        resMap.put("违章信息", violationInformationList);
        return R.ok(resMap);
    }

    @PostMapping("/delete/by/id")
    @ApiOperation("根据id删除某条违章信息")
    public R deleteById(@ApiParam("被删除数据的id") @RequestParam("id") Integer id){
        if (id == null) {
            return R.error(500106, "参数为空！");
        }

        ViolationInformation violationInformation = vioService.getById(id);
        vioService.delete(violationInformation);

        return R.ok();
    }

    @PostMapping("update/by/id")
    @ApiOperation("根据id修改违章记录的地点或原因【两个参数都要传、可以为空】,id不能为空")
    public R updateById(@ApiParam("数据的id") @RequestParam("id") Integer id,
                        @ApiParam("修改的地点place") @RequestParam(value = "place", required = false) String place,
                        @ApiParam("修改的原因reason") @RequestParam(value = "reason", required = false) String reason){
        if (id == null) {
            return R.error("id为空");
        }
        //通过id获取原来的数据
        ViolationInformation violationInformation = vioService.getById(id);
        //修改数据后更新数据库
        if (place != null) {
            violationInformation.setPlace(place);
        }
        if (reason != null) {
            violationInformation.setReason(reason);
        }
        vioService.updateById(violationInformation);
        return R.ok();
    }

    @GetMapping("/download/information/excel")
    @ApiOperation("以Excel表的格式下载违章信息，下载信息以天为单位选择")
    public void downloadViolationInformation(Admin admin, HttpServletResponse response,
                                             @ApiParam("筛选起始时间") @RequestParam("beginDay") String beginDay,
                                             @ApiParam("筛选结束时间") @RequestParam("endDay") String endDay){
        if (beginDay == null ||endDay == null) {
            return;
        }
        //定义时间格式精确到天
        DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        //将String类型转化为LocalDateTime
        LocalDateTime beginDay1 = LocalDateTime.parse(beginDay, dateTimeFormatter);
        LocalDateTime endDay1 = LocalDateTime.parse(endDay, dateTimeFormatter);
        List<ViolationInformation> violationInformationList = vioService.selectListByTime(beginDay1, endDay1, admin.getCampus());
        String fileName = "违章车辆信息";
        String sheetName = "违章车辆信息";
        try {
            //ViolationInformation.class是实体类
            //violationInformationList是需要导出的List实体类信息
            //导出车辆信息的excel文件
            ExcelUtil.writeExcel(response,violationInformationList,fileName,sheetName,ViolationInformation.class);
        } catch (Exception e) {
            log.error("导出文档出错：", e.getCause());
            e.printStackTrace();
            System.out.println("错误信息："+e.getMessage());
        }
    }

}

